﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;

using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

using SBPweb.RIA.Client.Common.Patterns;

using SBPweb.PlAzureNet.Client.Library.Controllers;
using SBPweb.PlAzureNet.Client.Library.Social;

namespace SBPweb.PlAzureNet.Client.Modules.Facebook.API
{

    public class Connector : ConnectorBase
    {

        #region Constants

        private const string ApplicationKey = "deb8ef2e27aca776a0e2be906f15714c";

        #endregion

        #region Fields

        private static SingletonHelper<Connector> shConnector = new SingletonHelper<Connector>();

        private BrowserSession session;
        private Api api;

        #endregion

        #region Constructor logic

        public Connector()
        {
            session = new BrowserSession(ApplicationKey, new Enums.ExtendedPermissions[] {
                Enums.ExtendedPermissions.photo_upload,
                Enums.ExtendedPermissions.status_update,
                Enums.ExtendedPermissions.share_item,
                Enums.ExtendedPermissions.create_event,
                Enums.ExtendedPermissions.create_note
            });
            session.CompressHttp = true;
            session.LoginCompleted += new EventHandler<AsyncCompletedEventArgs>(session_LoginCompleted);
            session.LogoutCompleted += new EventHandler<AsyncCompletedEventArgs>(session_LogoutCompleted);

            api = new Api(session);

            Friends = new API.Friends.FriendManager(api);
            Profiles = new API.Profiles.ProfileManager(api);
        }

        #endregion

        #region Properties

        public static Connector Instance
        {
            get
            {
                return shConnector.Instance;
            }
        }

        internal Dispatcher Dispatcher
        {
            get;
            set;
        }

        #endregion

        #region Methods

        public override void Login()
        {
            OnLoggingIn();
            session.Login();
        }

        public override void Logout()
        {
            OnLoggingOut();
            session.Logout();
        }

        #endregion

        #region Events

        public event EventHandler LoggingIn;

        public event EventHandler LoggedIn;

        public event EventHandler LoggingOut;

        public event EventHandler LoggedOut;

        #endregion

        #region Event handlers

        #region Session

        private void session_LoginCompleted(object sender, AsyncCompletedEventArgs e)
        {
            DataServiceController.Instance.FetchOnePersonByIds(1, session.UserId.ToString());
            //Friends.FetchFriends();

            //api.Profile.GetFBMLAsync(session.UserId, 1, new Profile.GetFBMLCallback(s), null);
            // GetInfoAsync(session.UserId, new Profile.GetInfoCallback(GetUserProfileCompleted), null);
            //api.Users.GetStandardInfoAsync(session.UserId.ToString(), new Users.GetStandardInfoCallback(GetUserInfoCompleted), null);
            api.Users.GetInfoAsync(session.UserId, new Users.GetInfoCallback(GetUserInfoCompleted), null);
        }

        private void session_LogoutCompleted(object sender, AsyncCompletedEventArgs e)
        {
            OnLoggedOut();
        }
        
        #endregion

        //#region Auth
        
        //private void auth_CreateTokenCompleted(string token, Object state, FacebookException e)
        //{
        //}

        //#endregion
        
        #region Users

        private void GetUserInfoCompleted(IList<user> users, Object state, FacebookException e)
        {
            if (e != null)
            {
                throw e;
            }

            this.User = new User(users.FirstOrDefault());

            OnLoggedIn();
        }

        #endregion

        #region Helpers

        protected virtual void OnLoggingIn()
        {            
            if (LoggingIn != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    LoggingIn(this, EventArgs.Empty);
                });
            }
        }

        protected virtual void OnLoggedIn()
        {
            if (LoggedIn != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    LoggedIn(this, EventArgs.Empty);
                });
            }
        }

        protected virtual void OnLoggingOut()
        {
            if (LoggingOut != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    LoggingOut(this, EventArgs.Empty);
                });
            }
        }

        protected virtual void OnLoggedOut()
        {
            if (LoggedOut != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                   LoggedOut(this, EventArgs.Empty);
                });
            }
        }

        #endregion

        #endregion

    }

}